package com.xhx.accounting;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class dayDetailActivity extends AppCompatActivity {
    private ListView dayDetailLv;
    private DBDao db;
    private List<Map<String, Object>> dataList;
    private dayDetailAdapter2 mAdapter;
    private Long timeStamp;
    private TextView date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_day_detail);

        Intent intent = getIntent();
        timeStamp = intent.getLongExtra("timeStamp",0);
        if (timeStamp != 0){
            Date date1 = new Date(timeStamp*1000);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            date = findViewById(R.id.date);
            date.setText(sdf.format(date1));

            dayDetailLv = findViewById(R.id.dayDetailLv);
            db = new DBDao(this);
            dataList = db.getDayRecord(timeStamp);
            Log.d("dayData", dataList.toString());
            mAdapter = new dayDetailAdapter2(this,dataList);
            dayDetailLv.setAdapter(mAdapter);
            dayDetailLv.setOnItemLongClickListener((parent, view, position, id) -> {
                //删除
                final AlertDialog.Builder deleteConfirm = new AlertDialog.Builder(dayDetailActivity.this);
                deleteConfirm.setTitle("确认删除此条记录吗？");
                deleteConfirm.setMessage("一经删除将无法恢复。");
                deleteConfirm.setPositiveButton("删除", (dialog, which) -> {
                    //数据库删除
                    HashMap<String, Object> recordInfo = (HashMap<String, Object>) dayDetailLv.getItemAtPosition(position);//SimpleAdapter返回Map
                    //Log.d("recordInfo",recordInfo.toString());
                    int recordId = (int)recordInfo.get("id");
                    Log.d("Delete", String.valueOf(recordId));
                    db.deleteRecord(recordId);
                    refresh();
                });
                deleteConfirm.setNegativeButton("取消", (dialog, which) -> {
                    //
                });
                deleteConfirm.show();
                return true;
            });

        } else {
//            Toast.makeText(this,  "Error", Toast.LENGTH_LONG).show();
            this.finish();
        }
    }
    private void refresh(){
        dataList.clear();
        dataList.addAll(db.getDayRecord(timeStamp));
        mAdapter.notifyDataSetChanged();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.arrow_left:
                finish();
                break;
        }
    }
}